Optimize unit_return_expecting_ord#14905
Conversation
|
rustbot has assigned @samueltardieu. Use |
samueltardieu
left a comment
There was a problem hiding this comment.
Nice, but you can use interned symbols instead of allocating strings, that can only help (if a little bit) with performances.
| fn_mut_trait: DefId, | ||
| ord_trait: Option<DefId>, | ||
| partial_ord_trait: Option<DefId>, | ||
| ) -> Vec<(usize, String)> { |
There was a problem hiding this comment.
| ) -> Vec<(usize, String)> { | |
| ) -> Vec<(usize, Symbol)> { |
| .any(|ord| Some(ord.self_ty()) == return_ty_pred.term.as_type()) | ||
| { | ||
| args_to_check.push((i, "Ord".to_string())); | ||
| args_to_check.push((i, String::from("Ord"))); |
There was a problem hiding this comment.
| args_to_check.push((i, String::from("Ord"))); | |
| args_to_check.push((i, sym::Ord)); |
| .any(|pord| pord.self_ty() == return_ty_pred.term.expect_type()) | ||
| { | ||
| args_to_check.push((i, "PartialOrd".to_string())); | ||
| args_to_check.push((i, String::from("PartialOrd"))); |
There was a problem hiding this comment.
| args_to_check.push((i, String::from("PartialOrd"))); | |
| args_to_check.push((i, sym::PartialOrd)); |
There was a problem hiding this comment.
I did it initially and reverted it with some mental gymnastics. I'll re-revert it.
7d0b957 to
2635ced
Compare
samueltardieu
left a comment
There was a problem hiding this comment.
Except for the two nits (no need to call .as_str(), and you can inline the trait_name in format string), this LGTM. If you just fix those two places feel free to r=me.
| format!( | ||
| "this closure returns \ | ||
| the unit type which also implements {}", | ||
| trait_name.as_str() |
There was a problem hiding this comment.
| format!( | |
| "this closure returns \ | |
| the unit type which also implements {}", | |
| trait_name.as_str() | |
| format!( | |
| "this closure returns \ | |
| the unit type which also implements {trait_name}" |
| format!( | ||
| "this closure returns \ | ||
| the unit type which also implements {}", | ||
| trait_name.as_str() | ||
| ), |
There was a problem hiding this comment.
| format!( | |
| "this closure returns \ | |
| the unit type which also implements {}", | |
| trait_name.as_str() | |
| ), | |
| format!( | |
| "this closure returns \ | |
| the unit type which also implements {trait_name}" | |
| ), |
This lint was previously written very clumsily, not shortcircuiting and doing a lot of unnecessary work. Now it makes sure to do the cheaper functions earlier and in general, just be smarter. (I specifically focused on minimizing binder instantiation) Also, avoid allocating unnecessarily
2635ced to
7e590de
Compare
Sadly we don't have bors anymore, so PRs can't be merged in behalf of another person 😢 |
|
I know, this is an inherited way of saying "feel free to press the merge button on my behalf as I take responsibility for it". But since I'm here I'll do it! |
| declare_lint_pass!(UnitReturnExpectingOrd => [UNIT_RETURN_EXPECTING_ORD]); | ||
|
|
||
| fn get_trait_predicates_for_trait_id<'tcx>( | ||
| // For each |
There was a problem hiding this comment.
Is this comment unfinished?
| { | ||
| preds.push(trait_pred); | ||
| .instantiate_bound_regions_with_erased(pred.kind().rebind(poly_trait_pred)); | ||
| for (i, tid) in trait_ids.iter().enumerate() { |
There was a problem hiding this comment.
The slice/array approach is a bit hard to understand, you have to look at the callsite to understand it. I see you want to get the operations within the instantiate_bound_regions_with_erased call. I think you could do that by passing in three separate arguments and handling them in sequence here, instead of using a for loop? I think that would be easier to read.
This lint was previously written very clumsily, not short-circuiting and doing a lot of unnecessary work.
Now it makes sure to do the cheaper functions earlier and in general, is just smarter.
(I specifically focused on minimizing binder instantiation
Sadly, I'm not finding any relevant result in a benchmark. Still with the LLVM coverage instruments, the expensive bits are called lots of less times (The binder instantiation that I care about is reduced from 95k to 10k throughout our test suite).
changelog:[
unit_return_expecting_ord]: Optimize the lint